#include <signal.h>
#include "udp_server.hpp"
using func_t = function<void(const string&name)>;

int fd = -1;


int main(int argc,char* argv[]){
    // if (argc != 3){
    //     Useage("Please use: ./clietn + server_ip + server_port");
    //     exit(1);
    // }
    fd = ::socket(AF_INET,SOCK_STREAM,0);
    if (fd < 0){
        LOG(ERROR) << "socket create error";
        exit(fd);
    }
    //初始化服务端信息, 注意这里不是用来bind的, 而是直接用来发送的
    
    string ip = argv[1];
    uint16_t port = stoi(argv[2]);  
    sockaddr_in server;
    server.sin_family = AF_INET;
    server.sin_port = ::htons(port);
    server.sin_addr.s_addr = ::inet_addr(ip.c_str());
    socklen_t len = sizeof(server);
    if (connect(fd,CONV(&server),len) < 0){
        LOG(ERROR) << "connect error";
        exit(1);
    }
    string messgae;
    while(1){
        LOG(INFO) << "Please enter";
        getline(cin,messgae);
        if (messgae.size() == 0) messgae += '\n';
        int n = write(fd,messgae.c_str(),messgae.size());
        if (n > 0){
            char inbuffer[1024];
            int ret = read(fd,inbuffer,sizeof(inbuffer));
            inbuffer[ret] = 0;
            LOG(INFO) << inbuffer;
        }
    }
    return 0;
}